package com.example.demo.controller;


import com.example.demo.entity.*;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.*;
import com.example.demo.service.NoticeService;
import com.example.demo.utils.ThreadLocalUtil;
import com.example.demo.vo.NoticeVo;
import com.example.demo.vo.Notice_temlateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeRepository noticeRepository;
    @Autowired
    NoticeService noticeService;
    @Autowired
    Notice_temlateRepository notice_temlateRepository;

    @PostMapping("/templateadd")
    PlannerResult<String> create_notice_template(@RequestBody Notice_temlateVo noticeTemlateVo){
        return noticeService.create_notice_template(noticeTemlateVo);
    };

    @PostMapping("/tamplateupdate")
    PlannerResult<String> update_notice_template(@RequestBody Notice_temlateVo noticeTemlateVo){
        return noticeService.update_notice_template(noticeTemlateVo);
    };

    @PostMapping ("/templatedelete/{template_id}")
    PlannerResult<String> delete_notice_template(@PathVariable ("template_id")Integer template_id){
        return noticeService.delete_notice_template(template_id);
    };

    @PostMapping("/createnotice")
    PlannerResult<String> create_notice(@RequestBody NoticeVo noticeVo){
        return noticeService.create_notice(noticeVo);
    };

    @PostMapping("/temlatelist")
    public PlannerResult<Page<Notice_temlate>> list_user(@RequestBody Map<String, Integer> params) {
        Integer page = params.get("page");
        Integer size = params.get("size");
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Notice_temlate> result = notice_temlateRepository.findAll(pageable);
        return PlannerResult.success(result);
    }

    @GetMapping ("/templates")
    PlannerResult<List<Notice_temlate>> count_notice()
    {
        List<Notice_temlate> notice_templates = notice_temlateRepository.findAll();
        return PlannerResult.success(notice_templates);
    };

    @GetMapping ("/countnotice")
    PlannerResult<List<Integer>> count_notice(@RequestParam("activity_id") Integer activity_id)
    {
        return noticeService.count_notice( activity_id);
    };

    @Autowired
    ActivityRepository activityRepository;
    @Autowired
    JoinerRepository joinerRepository;
    @Autowired
    Notice_recptionRepository noticeRecptionRepository;

    @PostMapping("/list")
    PlannerResult<List<NoticeVo>>  my_notice()
    {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<Notice_recption> noticeRecptions= noticeRecptionRepository.findByUserId(userId);

        List<Integer> ss = new ArrayList<>();
        for (Notice_recption noticeRecption:noticeRecptions
        ) {
            ss.add(noticeRecption.getNotice_id());
        }
        List<Notice> notices = noticeRepository.findAllById(ss);
        List<NoticeVo> noticeVos = new ArrayList<>();
        for (Notice notice:notices
        ) {
            NoticeVo noticeVo = new NoticeVo();
            noticeVo.setNotice_type_id(notice.getNotice_type_id());
            noticeVo.setId(notice.getId());
            noticeVo.setContext(notice.getContext());
            noticeVo.setSend_time(notice.getSend_time());
            noticeVo.setActivity_id(notice.getActivity_id());
            noticeVo.setState(noticeRecptionRepository.findByNoticeIdAndUserId(notice.getId(),userId).getState());
            noticeVos.add(noticeVo);
        }

        return PlannerResult.success(noticeVos);
    }
    @PostMapping("/new")
    PlannerResult<List<NoticeVo>> new_notice(){
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List <Joiner> joiners = joinerRepository.findByUser_id(userId);
        List<Integer> ids = new ArrayList<>();
        for (Joiner joiner:joiners
        ) {
            ids.add((joiner.getActivity_id()));
        }
        List<Notice> notices = noticeRepository.findNew(ids);
        List<NoticeVo> noticeVos = new ArrayList<>();
        for (Notice notice:notices
        ) {
            NoticeVo noticeVo = new NoticeVo();
            noticeVo.setNotice_type_id(notice.getNotice_type_id());
            noticeVo.setId(notice.getId());
            noticeVo.setContext(notice.getContext());
            noticeVo.setSend_time(notice.getSend_time());
            noticeVo.setActivity_id(notice.getActivity_id());
            System.out.println(notice.getId());
            System.out.println(userId);
            noticeVo.setState(noticeRecptionRepository.findByNoticeIdAndUserId(notice.getId(),userId).getState());
            noticeVos.add(noticeVo);
        }
        return PlannerResult.success(noticeVos);
    }

    @GetMapping("/readnotice")
    public PlannerResult<String> see_notice(@RequestParam("noticeid") Integer noticeid)
    {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        Notice_recption noticeRecption=noticeRecptionRepository.findByNoticeIdAndUserId(noticeid,userid);
        noticeRecption.setState("已读");
        Notice_recption result = noticeRecptionRepository.save(noticeRecption);
        if(result.getState().equals("已读"))
        {
            return PlannerResult.success("已查看");
        }
        else
        {
            return PlannerResult.error("error");
        }
    }
}

